package com.syq.controller;

import com.syq.annotation.RequiresPermissionsDesc;
import com.syq.pojo.*;
import com.syq.service.*;
import com.syq.util.ResponseUtil;
import com.syq.vo.CatVo;

import com.syq.vo.GoodsAllinone;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @ClassName: AdminGoods
 * @Description: TODO
 * @Create by: syq
 * @Date: 2022/7/22 21:57
 */

@RestController
@CrossOrigin
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    private DtsGoodsService dtsGoodsService;

    @Autowired
    private DtsGoodsProductService dtsGoodsProductService;

    @Autowired
    private DtsGoodsAttributeService dtsGoodsAttributeService;

    @Autowired
    private DtsGoodsSpecificationService dtsGoodsSpecificationService;

    @Autowired
    private DtsCategoryService dtsCategoryService;

    @Autowired
    private DtsBrandService dtsBrandService;


    //商品列表
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "查询")
    @RequiresPermissions(value = "admin:goods:list")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1")Integer page,
                       @RequestParam(defaultValue = "10")Integer limit,
                       @RequestParam(defaultValue = "add_time")String sort,
                       @RequestParam(defaultValue = "desc")String order){
        Map<String,Object> map=new HashMap<>();

        int pn=1;
        int ps=20;
        if (page!=0&&page>0){
            pn=page;
        }
        if (limit!=0&&limit>0){
            ps=limit;
        }

        Long total=dtsGoodsService.findCount();
        List<DtsGoods> goodList=dtsGoodsService.findAll(pn,ps,sort,order);

        map.put("total",total);
        map.put("items",goodList);
        return ResponseUtil.ok(map);
    }

    //商品详情
    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "详情")
    @RequiresPermissions(value = "admin:goods:detail")
    @GetMapping("/detail")
    public Object detail(Integer id){
        Map<String,Object> map=new HashMap<>();
        //查询商品
        DtsGoods dtsGoods=dtsGoodsService.findById(id);

        Integer categoryId = dtsGoods.getCategoryId();
        Integer pid=dtsCategoryService.findById(categoryId);

        int[] categoryIds={categoryId,pid};

        List<DtsGoodsProduct> products=dtsGoodsProductService.findById(id);

        List<DtsGoodsAttribute> attributes=dtsGoodsAttributeService.findById(id);

        List<DtsGoodsSpecification> specifications=dtsGoodsSpecificationService.findById(id);

        map.put("categoryIds",categoryIds);
        map.put("goods",dtsGoods);
        map.put("products",products.toArray());
        map.put("attributes",attributes.toArray());
        map.put("specifications",specifications.toArray());
        return ResponseUtil.ok(map);
    }

    //商品类别和供应商
    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        Map<String,Object> map=new HashMap<>();

        List<CatVo> brandList=dtsBrandService.findIdAndName();
        List<CatVo> categoryList=dtsCategoryService.findPid();

        map.put("brandList",brandList);
        map.put("categoryList",categoryList);
        return ResponseUtil.ok(map);
    }

    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "编辑")
    @RequiresPermissions(value = "admin:goods:update")
    @PostMapping("/update")
    public Object update(@RequestBody  GoodsAllinone goodsAllinone){
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        DtsGoods goods = goodsAllinone.getGoods();
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();

        dtsGoodsAttributeService.deleteById(goods.getId());
        dtsGoodsProductService.deleteById(goods.getId());
        dtsGoodsSpecificationService.deleteById(goods.getId());

        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goods.getId());
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            dtsGoodsAttributeService.add(attribute);
        }
        for (DtsGoodsProduct product : products) {
            product.setGoodsId(goods.getId());
            product.setAddTime(new Date());
            product.setUpdateTime(new Date());
            dtsGoodsProductService.add(product);
        }
        for (DtsGoodsSpecification specification : specifications) {
            specification.setGoodsId(goods.getId());
            specification.setAddTime(new Date());
            specification.setUpdateTime(new Date());
            dtsGoodsSpecificationService.add(specification);
        }
        dtsGoodsService.update(goods,goods.getId());

        return ResponseUtil.ok();
    }

    @RequiresPermissionsDesc(menu = {"商品管理","商品列表"},button = "删除")
    @RequiresPermissions(value = "admin:goods:delete")
    @GetMapping("/delete")
    public Object delete(@RequestBody DtsGoods dtsGoods){

        return dtsGoodsService.delete(dtsGoods);
    }

    @RequiresPermissionsDesc(menu = {"商品管理","商品上架"},button = "添加")
    @RequiresPermissions(value = "admin:goods:create")
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        dtsGoodsService.create(goodsAllinone);
        return ResponseUtil.ok();
    }
}
